<template>
  <!-- 行内布局 -->
  <el-row type="flex" justify="space-between" align="middle" style="height:40px; width:100%">
    <!--放置一个属性   这里的props和我们之前学习的父传子 的props没关系-->
    <el-col>
      <span>{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <!-- 下拉列表 -->
          <el-dropdown @command="handleCommand">
            <span type="primary" size="mini">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </span>
            <!-- 下拉列表项 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <div v-if="!isRoot">
                <el-dropdown-item command="edit">编辑子部门</el-dropdown-item>
                <el-dropdown-item command="del">删除子部门</el-dropdown-item>
              </div>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartment } from '@/api/departments'
export default {
  name: 'TreeTools',
  components: {},
  props: {
    treeNode: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {

    }
  },

  computed: {},
  methods: {
    handleCommand(type) {
      if (type === 'add') {
        this.$emit('addDepartment', this.treeNode)
        // console.log(type)
      } else if (type === 'edit') { // 打开编辑弹层
        this.$emit('editDept', this.treeNode)
        // console.log(type)
      } else {
        // 删除调用接口
        this.$confirm('确定要删除部门吗？').then(() => {
          return delDepartment(this.treeNode.id)
        }).then(() => {
          this.$emit('delDepartment')
          this.$message.success('删除成功')
        })
      }
    }
  }
}
</script>

<style lang='less' scoped>
</style>
